package com.exadel;

import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;


public class oddMoneyRun {
	
	static RealMoneyKeeper realKeeper = new RealMoneyKeeper();
	static EndlessMoneyKeeper endlessKeeper = new EndlessMoneyKeeper();
	static Calculation calc;
	static Scanner scanner = new Scanner(System.in);
    
    
	public static void main(String[] args) {
		UseDefault();
		MainMenu();	
	}
	public static void MainMenu(){
		System.out.println("   ----------MAIN------------------" );
		System.out.println("   |>-1.Choise Keeper Type  " );
		System.out.println("   |>-2.Edit Nominals  " );
		System.out.println("   |>-3.Enter money  " );
		System.out.print("   -->" );
		int pointer = enter();
		
		if(pointer==1)
			ChoiseKeeperType();
		else if(pointer ==2)
			editNominals();
		else if(pointer == 3)
			EnterMoney();
		else 
			MainMenu();
		
	}
	public static int enter(){
		int pointer = 0;
		try{
			pointer = scanner.nextInt();
		}
			catch(InputMismatchException e){
				scanner = new Scanner(System.in);
				System.out.print("  -->");
				enter();
				
		}
		return pointer;
	}
 	public static void EnterMoney(){
		System.out.println("" );
		System.out.println(" Enter count of money  " );
		System.out.print(" -->  " );
		int pointer = enter();
		try {
			calc.SetCountOfMoney(pointer);
			if(calc.Instance() == "RealMoneyKeeper"){
				System.out.println(" keeper have nominals before moneyOdd" );
				calc.PrintNominalls();
				System.out.println();
			}
			calc.ComputeOdd();
			System.out.println("-----------------------------------------");
			System.out.println(" keeper give  " );
			calc.PrintOdd();
			
			System.out.println("-----------------------------------------");
			if(calc.Instance() == "RealMoneyKeeper"){
				System.out.println(" keeper have nominals after moneyOdd" );
			}
			else{
				System.out.println(" current nominal" );
			}
			calc.PrintNominalls();
			System.out.println();
		} 
		catch (ImpossibleToGiveMoneyException e) {
			System.out.println(" inpossible to give current count of money  " );
			MainMenu();
		
		}
		catch (NoSuchElementException e){
			
			System.out.println(" error, keeper do`nt have this money" );
			calc.PrintNominalls();
			MainMenu();
		}
		
		catch (InvalidNumberException e) {
			System.out.println(" error, keeper do`nt have this money" );
			EnterMoney();
			//e.printStackTrace();
		} catch (NoNominalException e) {
			System.out.println(" error, keeper do`nt have this money" );
			EnterMoney();
			//e.printStackTrace();
		}
		catch (NullPointerException e){
			System.out.println(" error, keeper do`nt have this money" );
			EnterMoney();
		}
		
		MainMenu();
	}
	
	public static void editNominals(){
		System.out.println("" );
		System.out.println("---------------current nominalls---------------------" );
		calc.PrintNominalls();
		System.out.println("    |>-0. MainMenu  " );
		System.out.println("    |>-1. Enter nominal  " );
		if(calc.size() > 0 )
			System.out.println("    |>-2. Delete Nominal" );
		if(calc.Instance() == "RealMoneyKeeper")
			System.out.println("    |>-3.Enter Nominal and Count" );
		System.out.print("    -->" );
		int pointer = enter();
		if(pointer == 0)
			MainMenu();
		if(pointer == 1)
			EnterNominalls();
		else if(pointer ==2 & calc.size() > 0 )
			DeleteNominalls();
		else if(pointer == 3 & calc.Instance() == "RealMoneyKeeper")
			EnterCount();
		else editNominals();
	}
	public static void EnterCount(){
		
		int pointer = 1;
		for(;pointer!=0; ){
			System.out.println();
			System.out.println("  enter key for add or edit nominals" );
			System.out.print("  -->" );
			int key = enter();
			System.out.println("  enter count of nominals  '0' = break" );
			System.out.print("  -->" );
			pointer = enter();
			if(pointer == 0) MainMenu(); else
			try {
				calc.setCount(key,pointer);
				System.out.println();
				System.out.println("current nominalls" );
				calc.PrintNominalls();
				
			} catch (NullPointerException e){
				System.out.println("NullPointerException" );
				MainMenu();
			} catch (InvalidNumberException e) {
				System.out.println("InvalidNumberException " );
				MainMenu();
			}
		}
	}
	
	public static void EnterNominalls(){
		for(;true;){
			System.out.println();
			System.out.println("  -------------------enter nominals  '0' = break-----------" );
			System.out.print("  -->" );
			int pointer = enter();
			if(pointer == 0) MainMenu(); else
			try {
				calc.setNominal(pointer);
				System.out.println();
				System.out.println("current nominalls" );
				calc.PrintNominalls();
				
			} catch (InvalidNumberException e) {
				EnterNominalls();
			}
			catch (NullPointerException e){
				EnterNominalls();
			}
		}
	}
	public static void DeleteNominalls(){
		for(;true;){
			System.out.println();
			System.out.println("  delete nominals  '0' = break" );
			System.out.print("-->" );
			int pointer = scanner.nextInt();
			if(pointer == 0) MainMenu(); else
			
				calc.deleteNominal(pointer);
				System.out.println();
				System.out.println("  current nominalls" );
				calc.PrintNominalls();
				
			}
		
	}
	public static void ChoiseKeeperType(){
		int pointer = 0;
		System.out.println("" );
		System.out.println("    ------ChoiseKeeper Type--------" );
		System.out.println("    |>-1.Endless Keeper" );
		System.out.println("    |>-2.Real Keeper" );
		
		System.out.print  ("    -->" );
		while(true){
		pointer = enter();
		if(pointer == 1){
			System.out.println("    selected endlessKeeper" );
			calc = endlessKeeper;
			MainMenu();
		}
			
		else if (pointer ==2){
			System.out.println("    selected realKeeper" );
			calc = realKeeper;
			MainMenu();
		}
		}
	}
	
	
	public static void UseDefault(){
		try {
			endlessKeeper.setNominal(2,5,10,20,50,100,200,500);
		} catch (InvalidNumberException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		calc = endlessKeeper;
	}
	

	
}
